<?php

namespace common\logics;

use common\models\dynamic\DynamicVideo;
use Yii;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use common\models\dynamic\DynamicLikeLog;
use common\models\dynamic\DynamicStats;

/**
 * This is the model class for table "{{%topic}}".
 *
 * @property int $id
 * @property string $content 话题内容 ##  双井号匹配话题，话题保存时去除双井号
 * @property int $is_official 是否是官方话题 0否 1是
 * @property int $user_id 话题创建人
 * @property int $join_nums 话题参与数
 * @property string $pic_path 话题图片路径
 * @property int $weight 排序权重 由小到大，根据浏览量，点赞量动态变更..
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class TopicLogic extends \common\models\dynamic\Topic {

    public function getPublishTime() {
        if ($this->created_at) {
            return Yii::$app->formatter->asRelativeTime($this->created_at);
        }
        return '很久以前';
    }

    public function getFormatPicPath() {
        if ($this->pic_path) {
            return $this->pic_path;
        } else {
            return 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525429310524&di=fbb2e7173895c7a764a1042fa3c8b9d2&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F013ab659363b5ba8012193a39cdbfc.png%402o.jpg';
        }
    }

    public function fields() {
        return [
            'id',
            'content',
            'pic' => 'formatPicPath',
            'is_official' => function ($model) {
                return (boolean)$model->is_official;
            },
            'user',
            'join_nums',
            'views',
            'weight',
            'time' => 'publishTime',
            'created_at'
        ];
    }

}
